import json

from django import http
from django.contrib.auth.mixins import LoginRequiredMixin

from django.shortcuts import render
# Create your views here.
from django.views import View
from pure_pagination import Paginator

from courses.models import Course
from operation.models import UserCourse, UserFavorite, UserMessage
from organization.models import CourseOrg
from users.forms import UploadImageForm
from users.models import UserProfile
from organization.models import Teacher


class UserInfoView(View):
    def get(self, request):
        user = request.user
        context = {
            'nick_name': user.nick_name,
            'birthday': user.birthday,
            'address': user.address,
            'gender': user.gender,
            'mobile': user.mobile,
            'email': user.email,
            'image': user.image,
        }

        return render(request, 'usercenter-info.html', context)

    def post(self, request,):
        user = request.user


        # 接受数据
        nick_name = request.POST.get('nick_name')
        birthday = request.POST.get('birthday')
        address = request.POST.get('address')
        gender = request.POST.get('gender')
        mobile = request.POST.get('mobile')
        email = request.POST.get('email')

        # 2.1非空
        if not all([nick_name, birthday, address, gender, mobile, email]):
            return http.JsonResponse({'code': '', 'errmsg': '参数不完整'})
        user.nick_name = nick_name
        user.birthday = birthday
        user.address = address
        user.gender = gender
        user.mobile = mobile
        user.email = email
        user.save()
        return http.JsonResponse({
            "status": "success"
        })


class UploadImageView(View):
    """
    用户修改头像
    """

    def post(self, request):
        image_form = UploadImageForm(request.POST, request.FILES, instance=request.user)
        if image_form.is_valid():
            image_form.save()
            return http.JsonResponse({
                'status': 'success',
                'mag': '头像修改成功'
            })


class MyCourseView(View):
    def get(self, request):
        # 获取用户对象
        user = request.user
        # user_courses = UserCourse.objects.filter(user_id=user)
        user_courses = UserCourse.objects.filter(user=user)
        return render(request, 'usercenter-mycourse.html', {
            'user_courses': user_courses,
        })


class MyFavOrgView(View):
    def get(self, request):
        user = request.user
        # 查询对象
        orgs = UserFavorite.objects.filter(user_id=user.id, fav_type=2)
        # 构造数据
        org_list = []
        for org in orgs:
            org_list.append(CourseOrg.objects.get(pk=org.fav_id))
        return render(request, 'usercenter-fav-org.html', {
            'org_list': org_list,
        })


class MyFavTeacherView(View):
    def get(self, request):
        user = request.user
        # 查询对象
        teachers = UserFavorite.objects.filter(user_id=user.id, fav_type=3)
        # 构造数据
        teacher_list = []
        for teacher in teachers:
            teacher_list.append(Teacher.objects.get(pk=teacher.fav_id))
        return render(request, 'usercenter-fav-teacher.html', {
            'teacher_list': teacher_list,
        })


class MyFavCourseView(View):
    def get(self, request):
        user = request.user
        # 查询对象
        courses = UserFavorite.objects.filter(user_id=user.id, fav_type=1)
        # 构造数据
        courses_list = []
        for course in courses:
            courses_list.append(Course.objects.get(pk=course.fav_id))
        return render(request, 'usercenter-fav-course.html', {
            'course_list': courses_list,
        })


class MyMessageView(View):
    def get(self, request):
        user = request.user
        messages = UserMessage.objects.filter(user =user.id)
        page = request.GET.get('page')
        paginator = Paginator(messages, 1)
        if page is None or page == '':
            messages = paginator.page(1)
        else:
            messages = paginator.page(page)
        return render(request, 'usercenter-message.html', {
            'messages': messages,
        })
